/**
 * form表单
 */
seajs.use(["zui/form"],function(Form){
	var form1 = 
			new Form({srcNode:"#CM_Form",validType:"custom",validateFunc:function(ev){
				var field=ev.field;
				var msg=ev.validateMsg;
				if(msg==""){

				}else{
				  	App.showToast(msg);
			  	}
		  	}});
	$(".zaw-btn").on("click",function(){
		var upLen = $(".za_web_group_imgupload_img").find(".group_imgupload_view").length;
		if(form1.valid()){
			if( upLen < 4 ){
				App.showToast("您还有资料未上传");
			}else{
				$("#CM_Form").submit();
			}
		}
	});
});
//上传文件
$(document).on("change",".za_web_imgUP",function(){
	var maxLen = $(this).parents(".za_web_group_imgupload_img").attr("maxLen");
	var upLen = $(this).parents(".za_web_group_imgupload_img").find(".group_imgupload_view").length;
    var objUrl = getObjectURL(this.files[0]) ;
    if (objUrl) {
    	var newRm = '<div class="group_imgupload_view">'+
    						'<a class="za_web_img_close">X</a>'+
    						'<img src="'+objUrl+'">'+
    				'</div>';
    	$(this).parents(".imgupload_img_cell").before(newRm);
    	if( upLen < (maxLen - 1) ){
    		$(this).parents(".imgupload_room").show();
    	}else{
    		$(this).parents(".imgupload_room").hide();
    	}
    }
    $(this).val("");
});
$(document).on("click",".za_web_img_close",function(){
	var maxLen = $(this).parents(".za_web_group_imgupload_img").attr("maxLen");
	var upLen = $(this).parents(".za_web_group_imgupload_img").find(".group_imgupload_view").length;
	if( upLen <= maxLen ){
		$(".imgupload_room").show();
	}else{
		$(".imgupload_room").hide();
	}
	$(this).parents(".group_imgupload_view").remove();
});
/**
 * 建立一個可存取到該file的url
 * PS: 瀏覽器必須支援HTML5 File API
 */
function getObjectURL(file) {
    var url = null ; 
    if (window.createObjectURL!=undefined) { // basic
        url = window.createObjectURL(file) ;
    } else if (window.URL!=undefined) { // mozilla(firefox)
        url = window.URL.createObjectURL(file) ;
    } else if (window.webkitURL!=undefined) { // webkit or chrome
        url = window.webkitURL.createObjectURL(file) ;
    }
    return url ;
}